<?php
/* ******************************
	Main aJax actions
   ******************************
*/
	include('../../includes/classes/core.php');
	$action = $_REQUEST['act'];
	$error  = '';
	$sum	= 0;
	switch ($action) {
		case 'get_edit_page':
			$reserve_id = $_REQUEST['id'];
			$page 			= GetPage($reserve_id);
			$data			= array('page'	=> $page);
			
			break;
		case 'get_cartridge_type_list':
			$count = $_REQUEST['count'];
			$hidden = $_REQUEST['hidden'];
			
			$data = array(
					"aaData"	=> array()
			);
			
			$rResult = mysql_query("SELECT      `production`.`id`,
												`production`.`name`,
									            COUNT(*)
									FROM        `reserve`
									LEFT JOIN    `barcodes` ON `barcodes`.`code` = `reserve`.`barcode`
									LEFT JOIN			production ON production.id = reserve.production_id
									WHERE   NOT  ISNULL(`reserve`.`barcode`) AND `barcodes`.`actived` = 1 AND reserve.object_id = 133
									GROUP BY    `reserve`.`production_id`");
			
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count; $i++ )
				{
					// General output
					$row[] = $aRow[$i];
				}
					
				$data['aaData'][] = $row;
			}
			
			break;			
		case 'get_working_cartridge_list':
			$cartridge_id	= $_REQUEST['id'];
			$count		= $_REQUEST['count'];
			$hidden		= $_REQUEST['hidden'];
			
			$data = array(
					"aaData"	=> array()
			);
			
			if( $cartridge_id == '' ){
				$rResult = mysql_query("SELECT		`reserve`.`id`,
													`production`.`name`,
													`reserve`.`barcode`
										FROM        `reserve`
										LEFT JOIN    `barcodes` ON `barcodes`.`code` = `reserve`.`barcode`
										LEFT JOIN			production ON production.id = reserve.production_id
										WHERE   NOT  ISNULL(`reserve`.`barcode`) AND `barcodes`.`actived` = 1 AND reserve.object_id = 133 AND reserve.production_condition = 1");
				
			}else{				
				$rResult = mysql_query("SELECT		`reserve`.`id`,
													`production`.`name`,
													`reserve`.`barcode`
										FROM        `reserve`
										LEFT JOIN    `barcodes` ON `barcodes`.`code` = `reserve`.`barcode`
										LEFT JOIN			production ON production.id = reserve.production_id
										WHERE   NOT  ISNULL(`reserve`.`barcode`) AND `barcodes`.`actived` = 1 AND reserve.object_id = 133 AND reserve.production_condition = 1 AND `reserve`.`production_id` = '$cartridge_id'");
			}
			
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count; $i++ )
				{
					// General output
					$row[] = $aRow[$i];
				}
					
				$data['aaData'][] = $row;
			}
			
			break;
		case 'get_disworking_cartridge_list':
			$cartridge_id	= $_REQUEST['id'];
			$count		= $_REQUEST['count'];
			$hidden		= $_REQUEST['hidden'];
			
			$data = array(
					"aaData"	=> array()
			);
			
			if( $cartridge_id == '' ){
				$rResult = mysql_query("SELECT		`reserve`.`id`,
													`production`.`name`,
													`reserve`.`barcode`
										FROM        `reserve`
										LEFT JOIN    `barcodes` ON `barcodes`.`code` = `reserve`.`barcode`
										LEFT JOIN			production ON production.id = reserve.production_id
										WHERE   NOT  ISNULL(`reserve`.`barcode`) AND `barcodes`.`actived` = 1 AND reserve.object_id = 133 AND reserve.production_condition = 0");
				
			}else{				
				$rResult = mysql_query("SELECT		`reserve`.`id`,
													`production`.`name`,
													`reserve`.`barcode`
										FROM        `reserve`
										LEFT JOIN    `barcodes` ON `barcodes`.`code` = `reserve`.`barcode`
										LEFT JOIN			production ON production.id = reserve.production_id
										WHERE   NOT  ISNULL(`reserve`.`barcode`) AND `barcodes`.`actived` = 1 AND reserve.object_id = 133 AND reserve.production_condition = 0 AND `reserve`.`production_id` = '$cartridge_id'");
			}
			
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count; $i++ )
				{
					// General output
					$row[] = $aRow[$i];
				}
					
				$data['aaData'][] = $row;
			}
			
			break;
    default:
       $error =  "null";
}
	
	
$data['error'] = $error;

echo json_encode( $data );

function GetPage($reserve_id){
	$row = mysql_fetch_assoc( mysql_query("	SELECT      client.`name` as `client`,
											            production.`name`  as `model`,
											            IF(reserve.production_condition =0 , 'დასამუხტია', 'დამუხტულია') as con,
														`reserve`.`barcode` as `barcode`
											FROM        `reserve`
											LEFT JOIN	client ON client.id = SUBSTR(reserve.barcode, 2,4)
											LEFT JOIN	production ON production.id = SUBSTR(reserve.barcode, 6,4)
											WHERE		reserve.id = '$reserve_id'") );
	
	$data = '
		<div id="dialog-form">
		    <fieldset>
		    	<legend>კატრიჯების მიღება</legend>
				<table border="0" width="100%">
				<tr>
					<th>
						<label for="client" style="float: left;">კლიენტი :</label>
					</th>
					<th>
						<div id="client" style="float: left;">'.$row['client'].'</div>
					</th>
				</tr>
				<tr>
					<th>
						<div  style="margin-top: 12px; float: left;"><label for="model">მოდელი :</label></div>
					</th>
					<th>
						<div  style="margin-top: 12px; float: left;" id="model">'.$row['model'].'</div>
					</th>
				</tr>
				<tr >
					<th>
						<div  style="margin-top: 12px; float: left;"><label for="condition">მდგომარეობა :</label></div>
					</th>
					<th>
						<div  style="margin-top: 12px; float: left;" for="condition">'.$row['con'].'</div>
					</th>
				</tr>
				<tr >
					<th>
						<div  style="margin-top: 12px; float: left;"><label for="barcode">შტრიხკოდი :</label></div>
					</th>
					<th>
						<div  style="margin-top: 12px; float: left;" for="barcode">'.$row['barcode'].'</div>
					</th>
				</tr>								
			</table>
        </fieldset>
		<!-- ID -->
    </div>';
	return 	$data;	
}

?>